<template>
  <div style="min-height:300px">
  <el-upload class="upload-demo" :before-upload="beforeAvatarUpload" :on-success="getUploadResult"
      :action="this.$http.defaults.baseURL + 'shenghuohui/uploadfile/' + this.$route.params.id" with-credentials accept="application/pdf" :limit="1" :before-remove="removeFile" :file-list="myfileList" :on-exceed="handleExceed" style="width:400px" >
    <el-button type="primary" icon="el-icon-document">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传PDF文件</div>
  </el-upload>
  </div>
</template>
<script>
export default {
  components: {
  },
  props: {
    fileList: {
      type: Array
    },
    timestamp: ''
  },
  data () {
    return {
      myfileList: []
    }
  },
  watch: {
  },
  methods: {
    handleExceed () {
      this.$message.warning(`请先删除以后再重新上传`)
    },
    removeFile (file) {
      const isPDF = file.name.indexOf('.pdf') > 0
      if (!isPDF) {
        return
      }
      var _this = this
      this.$http.post(`/shenghuohui/removefile/` + this.$route.params.id).then(function (response) {
        if (response.data.IsSuccess === true) {
          console.log('删除文件成功')
          _this.$message({
            message: '删除成功！',
            type: 'success'
          })
          _this.$emit('refreshevent')
        } else {
          console.log('删除文件失败:' + response.data.Msg)
          _this.$message({
            message: '删除文件失败:' + response.data.Msg,
            type: 'error'
          })
        }
      }).catch(function (error) {
        console.log(error)
        _this.$message({
          message: '获取失败:' + error,
          type: 'error'
        })
      })
    },
    getUploadResult (res) {
      if (res.IsSuccess === true) {
        this.$message({
          message: '上传成功！',
          type: 'success'
        })
        this.$emit('refreshevent')
      } else {
        console.log(res.Msg)
        this.$message({
          message: '上传失败:' + res.Msg,
          type: 'error'
        })
      }
    },
    beforeAvatarUpload (file) {
      console.log('文件类型')
      console.log(file.name)
      const isPDF = file.name.indexOf('.pdf') > 0
      // const isLt20M = file.size / 1024 / 1024 < 20
      if (!isPDF) {
        this.$message.error('上传文件只能是 PDF 格式!')
      }
      // if (!isLt20M) {
      //   this.$message.error('上传文件大小不能超过 20MB!')
      // }
      return isPDF
    }
  },
  mounted () {
    console.log('子页面检测timestamp是否赋值' + this.timestamp)
    console.log('子页面检测fileList是否赋值' + this.fileList)
    this.myfileList = this.fileList
  }
}
</script>
<style scoped>
</style>
